package main

import (
	"encoding/json"
	"fmt"
	"gopkg.in/goyy/goyy.v0/comm/log"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"
)

func main() {
	rurl := "http://10.106.89.81:8080/product/yxhn_rec.json"
	rurl = "http://localhost:9091"
	reqData := make(map[string]string)
	reqData["appkey"] = "yuwenbn"
	fmt.Println(rurl, reqData)
	//data, state :=  HttpGet(rurl, reqData)
	data := []byte(`{"products":[{"type":0,"updatetime":"2015-10-28 15:35:53","product_url":"/product/dm/MzIx/16053192?stid=CAF1A3DFB532105FFED0D024130F58C5CFA","product_name":"数据测试NL","annua_rate":10.0,"min_amount":1,"start_sell":0,"call_date":"2016-05-05","expire_period":null,"expire_period_unit":null},{"type":0,"updatetime":"2015-10-28 15:35:53","product_url":"/product/dm/MzIx/17043935?stid=CAF1A3DFB532105FFED0D024130F58C5CFA","product_name":"球友1号-美洲豹","annua_rate":8.0,"min_amount":3000000,"start_sell":0,"call_date":"2017-04-29","expire_period":null,"expire_period_unit":null},{"type":1,"updatetime":"2015-10-28 15:35:53","product_url":"/product/dm/MzIx/16023859?stid=CAF1A3DFB532105FFED0D024130F58C5CFA","product_name":"wlm-财1测试2","annua_rate":11.3,"min_amount":400,"start_sell":0,"call_date":"2016-02-01","expire_period":null,"expire_period_unit":null}],"resCode":"1","resInfo":"success"}`)
	state := 200
	if state == 200 {
		mmap := JsonToMap(string(data))
		fmt.Println(mmap)
	} else {
		fmt.Println("-11111111111")
	}
}

var client = &http.Client{}

//get请求
func HttpGet(rurl string, reqData map[string]string) (string, int) {
	if reqData == nil {
		reqData = make(map[string]string)
	}
	return httpRequest("GET", rurl, reqData)
}

//Post请求
func HttpPost(rurl string, reqData map[string]string) (string, int) {
	if reqData == nil {
		reqData = make(map[string]string)
	}
	return httpRequest("POST", rurl, reqData)
}

//post请求
func httpRequest(method, rurl string, reqData map[string]string) (string, int) {
	var param, body string
	var strReader *strings.Reader
	if method == "POST" {
		values := url.Values{}
		for k, v := range reqData {
			values.Add(k, v)
		}
		param = values.Encode()
		strReader = strings.NewReader(param)
	} else if method == "GET" {
		var i = 0
		for k, v := range reqData {
			if i > 0 {
				param += "&"
			}
			i++
			param += k + "=" + v
		}
		rurl += "?" + param
		strReader = strings.NewReader("")
	}
	//向服务端发送请求
	request, err := http.NewRequest(method, rurl, strReader)
	if method == "POST" {
		request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	}
	if err != nil {
		log.Errorln(err.Error())
		return body, 100
	}
	response, err := client.Do(request)
	if err != nil {
		log.Errorln(err.Error())
		return body, 118
	}
	defer response.Body.Close()
	status := response.StatusCode
	if status == 200 {
		str, _ := ioutil.ReadAll(response.Body)
		body = string(str)
		return body, status
	}
	//测试数据
	body = `{"products":[{"type":0,"updatetime":"2015-10-28 15:35:53","product_url":"/product/dm/MzIx/16053192?stid=CAF1A3DFB532105FFED0D024130F58C5CFA","product_name":"数据测试NL","annua_rate":10.0,"min_amount":1,"start_sell":0,"call_date":"2016-05-05","expire_period":null,"expire_period_unit":null},{"type":0,"updatetime":"2015-10-28 15:35:53","product_url":"/product/dm/MzIx/17043935?stid=CAF1A3DFB532105FFED0D024130F58C5CFA","product_name":"球友1号-美洲豹","annua_rate":8.0,"min_amount":3000000,"start_sell":0,"call_date":"2017-04-29","expire_period":null,"expire_period_unit":null},{"type":1,"updatetime":"2015-10-28 15:35:53","product_url":"/product/dm/MzIx/16023859?stid=CAF1A3DFB532105FFED0D024130F58C5CFA","product_name":"wlm-财1测试2","annua_rate":11.3,"min_amount":400,"start_sell":0,"call_date":"2016-02-01","expire_period":null,"expire_period_unit":null}],"resCode":"1","resInfo":"success"}`
	return body, status
}

//转换map
func JsonToMap(data string) map[string]interface{} {
	var object interface{}
	json.Unmarshal([]byte(data), &object)
	if mmap, ok := object.(map[string]interface{}); ok {
		for key, val := range mmap {

		}
		return mmap
	}
	return make(map[string]interface{})
}
